package org.electrical.controller.system;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.electrical.dao.EleUserDao;
import org.electrical.pojo.ElePermission;
import org.electrical.pojo.EleRole;
import org.electrical.pojo.EleUser;
import org.electrical.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
@CrossOrigin(value = "*")
public class EleUserController {

    @Autowired
    private EleUserDao eleUserDao;

    //  登录
    @PostMapping("/login")
    @ResponseBody
    public ResponseResult login(String eleAccount, String password){

        EleUser eleUser = eleUserDao.getByEleAccount(eleAccount);
        //  判断用户是否存在
        if (eleUser == null) {
            return new ResponseResult(1, "用户账号或密码错误");
        }
        //  比对密码
        String pass_str = new Md5Hash(password, eleUser.getSalt()).toString();
        if (!pass_str.equals(eleUser.getPassword())) {
            return new ResponseResult(1, "用户账号或密码错误");
        }
        //  获取用户所拥有角色的权限
        List<ElePermission> permissions = eleUserDao.getPersByUserId(eleUser.getUserId());
        //  登录成功之后，返回数据
        Map<String, Object> map = new HashMap<>();
        map.put("user", eleUser);
        map.put("user_pers", permissions);
        return new ResponseResult(map);

    }

    @RequestMapping("byUserId")
    @ResponseBody
    public ResponseResult byUserId(int id) {

        EleUser eleUser = eleUserDao.getById(id);
        return new ResponseResult(eleUser);

    }

    @RequestMapping("/list")
    @ResponseBody
    public ResponseResult<List<EleUser>> list(@RequestParam(defaultValue = "1")int page, @RequestParam(defaultValue = "5")int limit, EleUser condition, String roleIds) {

        //将传递过来的角色编号封装到user --> sysRoles  --> id
        if (StringUtils.isNotEmpty(roleIds)) {
            String[] args = roleIds.split(",");
            List<EleRole> eleRoles = new ArrayList<>();
            for (String arg : args) {
                EleRole eleRole = new EleRole();
                eleRole.setRoleId(Integer.parseInt(arg));
                eleRoles.add(eleRole);
            }
            condition.setEleRoles(eleRoles);
        }

        Page<EleUser> pager = PageHelper.startPage(page, limit);
        List<EleUser> list = eleUserDao.getConditionList(condition);

        return new ResponseResult(pager.getTotal(), list);

    }

    @RequestMapping("/add")
    @ResponseBody
    public ResponseResult add(EleUser eleUser, String roleIds) throws Exception {

        String pass_str = new Md5Hash(eleUser.getPassword(), eleUser.getSalt()).toString();
        eleUser.setPassword(pass_str);
        eleUserDao.add(eleUser);
        //  添加角色关系
        String[] array = roleIds.split(",");
        int[] ids = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            ids[i] = Integer.parseInt(array[i]);
        }
        eleUserDao.addUserRole(eleUser.getUserId(), ids);
        return ResponseResult.createSuccessResult();
    }

    @RequestMapping("/delete")
    @ResponseBody
    public ResponseResult delete(int id) throws Exception {

        eleUserDao.delete(id);
        return ResponseResult.createSuccessResult();

    }

    @RequestMapping("/batchDelete")
    @ResponseBody
    public ResponseResult batchDelete(Integer[] ids) throws Exception {

        eleUserDao.batchDelete(ids);
        return ResponseResult.createSuccessResult();

    }

    @RequestMapping("/update")
    @ResponseBody
    public ResponseResult update(EleUser eleUser, String roleIds) throws Exception {

        String pass_str = new Md5Hash(eleUser.getPassword(), eleUser.getSalt()).toString();
        eleUser.setPassword(pass_str);
        eleUserDao.update(eleUser);
        //  添加角色关系
        String[] array = roleIds.split(",");
        int[] ids = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            ids[i] = Integer.parseInt(array[i]);
        }
        eleUserDao.deleteByUserRole(eleUser.getUserId());
        eleUserDao.addUserRole(eleUser.getUserId(),ids);
        return ResponseResult.createSuccessResult();
    }

    //  查询客户经理
    @RequestMapping("byRoleId")
    @ResponseBody
    public ResponseResult<List<EleUser>> byRoleId() {

        List<EleUser> eleUsers = eleUserDao.getByRoleId(4);
        return new ResponseResult(eleUsers);

    }

}
